package saas.adapter.sdk.core.context;

import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import saas.adapter.sdk.entity.TenantConversionRelation;
import saas.adapter.sdk.init.TenantConversionRelationInitializer;

import java.util.Objects;

/**
 * @title: OutboundConversionTenantContext
 * @description: 出口转换租户上下文
 * @author: guan.xin
 * @create: 2021-11-16 19:03
 * @since: JDK8
 */
@Slf4j
public class OutboundConversionTenantContext {

    /**
     * 根据店铺编码设置租户上下文
     *
     * @param storeCode
     */
    public static void setTenantContextByStoreCode(String storeCode) {
        TenantConversionRelation tenantConversionRelation = TenantConversionRelationInitializer.getTenantConversionRelationByStoreCode(storeCode);
        // 映射配置校验（warn）
        if (Objects.isNull(tenantConversionRelation)
                || Strings.isNullOrEmpty(tenantConversionRelation.getMpTenantCode())) {
            log.warn("storeCode：{}, 无法映射中台租户信息", storeCode);
        } else {
            log.info("storeCode：{}, 成功映射中台租户信息, 中台租户：{}", storeCode, tenantConversionRelation.getMpTenantCode());
            // 设置租户转换关系上下文
            TenantConversionRelationHolder.set(tenantConversionRelation);
        }
    }

    /**
     * 根据店铺编码设置租户上下文
     *
     * @param storeCode
     */
    public static void setTenantContextByStoreCode(String storeCode, boolean inheritable) {
        TenantConversionRelation tenantConversionRelation = TenantConversionRelationInitializer.getTenantConversionRelationByStoreCode(storeCode);
        // 映射配置校验（warn）
        if (Objects.isNull(tenantConversionRelation)
                || Strings.isNullOrEmpty(tenantConversionRelation.getMpTenantCode())) {
            log.warn("storeCode：{}, 无法映射中台租户信息", storeCode);
        } else {
            log.info("storeCode：{}, 成功映射中台租户信息, 中台租户：{}", storeCode, tenantConversionRelation.getMpTenantCode());
            // 设置租户转换关系上下文
            TenantConversionRelationHolder.set(tenantConversionRelation);
        }
    }

    /**
     * 根据库存主体编码设置租户上下文
     *
     * @param inventorySubjectCode
     */
    public static void setTenantContextByInventorySubjectCode(String inventorySubjectCode) {
        TenantConversionRelation tenantConversionRelation = TenantConversionRelationInitializer.getTenantConversionRelationByStoreCode(inventorySubjectCode);
        // 映射配置校验（warn）
        if (Objects.isNull(tenantConversionRelation)
                || Strings.isNullOrEmpty(tenantConversionRelation.getMpTenantCode())) {
            log.warn("inventorySubjectCode：{}, 无法映射中台租户信息", inventorySubjectCode);
        } else {
            log.info("inventorySubjectCode：{}, 成功映射中台租户信息, 中台租户：{}", inventorySubjectCode, tenantConversionRelation.getMpTenantCode());
            // 设置租户转换关系上下文
            TenantConversionRelationHolder.set(tenantConversionRelation);
        }
    }

    /**
     * 根据库存主体编码设置租户上下文
     *
     * @param inventorySubjectCode
     */
    public static void setTenantContextByInventorySubjectCode(String inventorySubjectCode, boolean inheritable) {
        TenantConversionRelation tenantConversionRelation = TenantConversionRelationInitializer.getTenantConversionRelationByStoreCode(inventorySubjectCode);
        // 映射配置校验（warn）
        if (Objects.isNull(tenantConversionRelation)
                || Strings.isNullOrEmpty(tenantConversionRelation.getMpTenantCode())) {
            log.warn("inventorySubjectCode：{}, 无法映射中台租户信息", inventorySubjectCode);
        } else {
            log.info("inventorySubjectCode：{}, 成功映射中台租户信息, 中台租户：{}", inventorySubjectCode, tenantConversionRelation.getMpTenantCode());
            // 设置租户转换关系上下文
            TenantConversionRelationHolder.set(tenantConversionRelation);
        }
    }
}
